﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

using PMServiceDesk.Parameters.Constantes;

namespace PMServiceDesk.Utils.ManejoDatos
{
    public static class ConvertFormat
    {
        public static string Date_To_Char(Object value, string format = Formatos.Fecha_Default, string culture = Formatos.Culture_EsPe)
        {
            if (!ConvertFormat.CheckValue(value)) return "";

            return Convert.ToDateTime(value).ToString(format,new CultureInfo(culture));
        }

        public static string Decimal_To_Char(Object value, string format = Formatos.Decimal_Default)
        {
            if (!ConvertFormat.CheckValue(value)) return "";

            string cadena = value.ToString().Trim();

            return Convert.ToDecimal(cadena).ToString(format);
        }

        public static decimal Char_To_Decimal(Object value, string culture = Formatos.Culture_EsPe)
        {
            if (!ConvertFormat.CheckValue(value)) return 0;

            string cadena = value.ToString().Trim();

            return Decimal.Parse(cadena, new CultureInfo(culture));
        }

        public static decimal Char_To_Integer(Object value)
        {
            if (!ConvertFormat.CheckValue(value)) return 0;

            string cadena = value.ToString().Trim();

            return Convert.ToInt32(cadena);
        }

        public static DateTime Char_To_Date(Object value, string format = Formatos.Fecha_Default, string culture = Formatos.Culture_EsPe)
        {
            if (!ConvertFormat.CheckValue(value)) return new DateTime();

            string cadena = value.ToString().Trim();

            return DateTime.ParseExact(cadena,format,new CultureInfo(culture));
        }

        public static object Char_To_DateOrNull(Object value, string format = Formatos.Fecha_Default, string culture = Formatos.Culture_EsPe)
        {
            if (!ConvertFormat.CheckValue(value)) return null;

            return ConvertFormat.Char_To_Date(value, format, culture);
        }

        public static string Char_To_StringOrNull(Object value)
        {
            if (!ConvertFormat.CheckValue(value)) return null;

            return value.ToString();
        }

        public static bool CheckValue(Object value)
        {
            if (value == null) return false;

            if (value == DBNull.Value) return false;

            if (value.ToString().Trim() == "") return false;

            return true;
        }
            
    }
}
